#include "Disk.h"
#include "Ray.h"
#include "ContactData.h"

bool Disk::Hit(const Ray& ray, float& tmin, ContactData& data) const
{
    Vec3 pointToOrigin = m_pos - ray.m_origin;
    float directionDotNormal = ray.m_dir.DotProduct(m_normal);
    Vec3 normalOverDot =  m_normal / directionDotNormal;
    float t = pointToOrigin.DotProduct(normalOverDot);

    if(t <= 0.001f)
        return false;

    Vec3 point = ray.m_origin + (ray.m_dir * t);

    if((m_pos-point).squareMagnitude() < m_rad*m_rad)
    {
        tmin = t;
        data.m_hitNormal = m_normal;
        data.m_hitPoint = point;
        data.m_hitTarget = (Renderable*)this;
        return true;
    }
    return false;
}